package slim3.demo.controller.six;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;



public class AirportStatus {
	
    public static void main(String[] args) throws Exception {

//        AirportStatus test = new AirportStatus();
    }
    
    
    public ArrayList<AirportBean> getAirportStatus() throws Exception{
//    public void getWeather() throws Exception {


        String data_original;
        String[] data_original_array;
        String[] data_array;
        ArrayList<AirportBean> result = new ArrayList<AirportBean>();
        AirportBean bean;
        
        
        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://aoaws.caa.gov.tw/AWS/mainRight.php?lang=tw");
//                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          BufferedReader in = new BufferedReader(new FileReader("airport2.txt"));
          
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
              
                if (inputLine.contains("<script language=\"JavaScript\">addarray")) {
                  
                    inputLine = inputLine.substring(inputLine.indexOf("<script language=\"JavaScript\">addarray"), inputLine.indexOf("(RCYU)</option>")+15);
//                  System.out.println(inputLine);
                  
                      data_original = inputLine;
                          
                      //split string
                      data_original_array = data_original.split(("<script language=\"JavaScript\">"));                        
    //                      for (int i=1; i<data_original_array.length; i++) {                
    //                          System.out.println(data_original_array[i]);                
    //                      }
                      
                      //get pure info
                      for (int i=1; i<data_original_array.length; i++) {    
                          
                          data_original_array[i] = data_original_array[i].substring(data_original_array[i].indexOf("array(")+6, data_original_array[i].indexOf(")</script"));
    //                          System.out.println(data_original_array[i]);
                          
                          data_array = data_original_array[i].split(",");
                          
                          for (int j=0; j<data_array.length; j++) {                
                              data_array[j] = data_array[j].substring(1, data_array[j].length()-1);
    //                              System.out.println(data_array[j]);
                          }
                          
                          bean = new AirportBean();
                          
                          //set airport
                          bean.setAirport(data_array[2]);
                          if (bean.getAirport().equals("RCTP")) {
                              bean.setAirport("桃園");
                          } else if (bean.getAirport().equals("RCSS")) {
                              bean.setAirport("台北松山");
                          } else if (bean.getAirport().equals("RCKH")) {
                              bean.setAirport("高雄");
                          } else if (bean.getAirport().equals("RCFN")) {
                              bean.setAirport("台東");
                          } else if (bean.getAirport().equals("RCKW")) {
                              bean.setAirport("恆春");
                          } else if (bean.getAirport().equals("RCBS")) {
                              bean.setAirport("金門");
                          } else if (bean.getAirport().equals("RCMT")) {
                              bean.setAirport("北竿");
                          } else if (bean.getAirport().equals("RCFG")) {
                              bean.setAirport("南竿");
                          } else if (bean.getAirport().equals("RCGI")) {
                              bean.setAirport("綠島");
                          } else if (bean.getAirport().equals("RCLY")) {
                              bean.setAirport("蘭嶼");
                          } else if (bean.getAirport().equals("RCQC")) {
                              bean.setAirport("澎湖馬公");
                          } else if (bean.getAirport().equals("RCCM")) {
                              bean.setAirport("澎湖七美");
                          } else if (bean.getAirport().equals("RCWA")) {
                              bean.setAirport("澎湖望安");
                          } else if (bean.getAirport().equals("RCMQ")) {
                              bean.setAirport("台中清泉崗");
                          } else if (bean.getAirport().equals("RCKU")) {
                              bean.setAirport("嘉義");
                          } else if (bean.getAirport().equals("RCNN")) {
                              bean.setAirport("台南");
                          } else if (bean.getAirport().equals("RCYU")) {
                              bean.setAirport("花蓮");
                          } else {
                              //ignore, stay ICAO code
                          }
                          
                          
                          //set temperature
                          if (data_array[8].equals("1") ||
                              data_array[7].equals("1")   ) {
                              
                              String temp = data_array[19];
                              temp = temp.substring(0, 2);
                              bean.setTemperature(temp);
                              
                          }
                          
                          
                          //set canBeSeenDistance
                          if (data_array[8].equals("1") ||
                              data_array[7].equals("1")   ) {
                              
                              String temp = data_array[14];
                              temp = temp.substring(temp.indexOf("\'>")+2, temp.indexOf("</f"));
                              bean.setCanBeSeenDistance(temp);
                              
                          }
                          
                          
                          
                          //set time and status
                          if (data_array[9].equals("1")) {
                              
                              bean.setTime(data_array[11].split(" ")[2]);
                              bean.setStatus("已於今日"+bean.getTime()+"關場（航班已結束）");
                              bean.setStatusCode("4");
                              
                          } else if (data_array[8].equals("1")) {
                              
                              bean.setTime(data_array[11].split(" ")[1]);
                              bean.setStatus("已於今日"+bean.getTime()+"關場（天氣影響）");
                              bean.setStatusCode("3");
                              
                          } else if (data_array[7].equals("1")) {
                              
                              bean.setStatus("天候不佳，能見度" + bean.getCanBeSeenDistance()
                                  + "公尺，氣溫" + bean.getTemperature() + "ºC"); //可能隨時關場（天氣影響）
                              bean.setStatusCode("2");
                              
                          } else {
                              
                              bean.setStatus("正常起降");
                              bean.setStatusCode("1");
                              
                          }
    
                          result.add(bean);
                          
                      }
                      
                }
                  
            }
            
            in.close();
                    
            for (int i=0; i<result.size(); i++) {
                System.out.println(result.get(i).toString());
            }
            
            
            //松山：正常起降
            //高雄：可能隨時關場(氣候因素)
            //蘭嶼：已於time關場(氣候因素)
            //金門：已於time關場(今日航班已結束)
                        
            
            } catch (IOException e) {
                e.getStackTrace();
            }
        
        return result;
      
            
    }
    
    
}
